const getComponents = (instance, name) => {
  return new Promise((resolve) => {
    let components = {};
    const deep = (child) => {
      let comps = child.component
        ? [child.component]
        : child.components ||
          (child.subTree ? [child.subTree] : null) ||
          (child.children && child.children instanceof Array
            ? [...child.children]
            : []);

      if (comps) {
        if (comps && comps instanceof Array) {
          comps.forEach((a) => {
            deep(a);
          });
        }
        Object.values(comps).forEach((a) => {
          if (a.name && a.name === name) {
            components[a.name] = a;
            console.log(components, a.name, "00000");
          }
        });
        return components;
      }
    };
    instance.ctx.$nextTick(() => {
      const comps = deep(instance);
      resolve(comps);
    });
  });
};

export default {
  async dispatch(instance, componentName, eventName, params) {
    const comp = await getComponents(instance, componentName);
    comp[componentName]["methods"][eventName](params);
  },
};
